﻿using FlyIL;
using FlyIL.Emu;
using FlyIL.Lifter;
using FlyIL.Optimizer;
using FlyRemote.Base;
using FlyRemote.Remote;
using System;

namespace Console1
{

    class Program
    {


        static void _init(string _process)
        {



            var wp = PAFactory.Default;


            var list = wp.GetProcesses();
            foreach (var u in list)
            {
                if (u.ProcessName.Equals(_process, comparisonType: StringComparison.CurrentCultureIgnoreCase))
                {
                    wp.OpenedProcess = u;
                    break;
                }
            }


            //var mod = def.GetModules();

            //var addr = def.ResolveModule(_process);


        }



        static void TestSp()
        {

            _init("pt.sp");

            Snapshot sp = SnapshotMaker.Create(DataType.Empty, SnapshotRetrievalMode.FromExe);


            FuckSp.Detector detector = new FuckSp.Detector(sp.ReadGroups[0]);

            IReader reader = new MyReader(sp.ReadGroups[0]);

            detector.Detecter(false, 0x04232145, reader, null);


            var data = detector.Compile(0x401000, new MyReport());


            RMFactory.Default.WriteBytes(0x401000, data);

            var nop = new byte[10];
            for (int i = 0; i < nop.Length; i++)
            {
                nop[i] = 0x90;
            }

            RMFactory.Default.WriteBytes(0x401000 + (uint)data.Length, nop);


        }




        static void TestVmp()
        {
            _init("xiadan");

            Snapshot sp = SnapshotMaker.Create(DataType.Empty, SnapshotRetrievalMode.FromExe);

            var detector = new FuckVmp.VMP.Detector(PAFactory.Default.OpenedProcess.Handle, sp.ReadGroups[0]);
            /// 提取代码所需 
            IReader reader = new MyReader(sp.ReadGroups[0]);

            detector.Reader = reader;

            detector.Symbol = new MySymbolHandler(PAFactory.Default.OpenedProcess.Handle); 


            detector.Search();

            foreach (var u in detector.VMIats)
            {
                Console.WriteLine(u);
            }


            //var cpu = new Cpu(false, reader);
            //var cpu2 = new Cpu(false, reader);

            //var lifter = new Lifters.Intel(false, reader);
            //var config = new Lifters.IntelConfig { CallAsjmp = true, CheckRet = false };

            //foreach (var u in list)
            //{
            //    Context context = new Context();

            //    lifter.Collect(context, u.BaseAddress, config);

            //    context.Dump();

            //    cpu.Reset();
            //    cpu.Run(context);

            //    ulong next = cpu.NextIp;


            //    context.SimplifyFlow();


            //    cpu2.Reset();
            //    cpu2.Run(context);

            //    ulong next2 = cpu2.NextIp;

            //    if (next2 != next)
            //    {
            //        throw new Exception();
            //    }


            //    context.DumpFlow();

            //}

        }

        static void Main(string[] args)
        {

            //TestSp();


            TestVmp();



            #region 测试提升器及编译器

            //var lego = new MyReader( sp.ReadGroups[0]);

            //var lifter = new Lifters.Intel(32, lego  );

            //Vcontext vcon = new Vcontext(32);
            //lifter.Collect(vcon, 0x004149D0, new Lifters.IntelConfig());

            //vcon.Dump();

            //var ass = Jit.Intel.Compile(vcon, 0x500000);
            //ass.dump();

            #endregion



            #region 化简测试



            #endregion




        }
    }


}
